package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxAdGroup extends AdGroup {
    private long gxId;

    public GxAdGroup(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("AdDistribution").getLength() > 0 &&
            element.getElementsByTagName("AdDistribution").item(0).getChildNodes().getLength() > 0) {
            String[] adDistributions = new String[1];
            adDistributions[0] = ((Node) element.getElementsByTagName("AdDistribution").item(0).getChildNodes().item(0)).getNodeValue();
            this.setAdDistribution(adDistributions);
        }
        if (element.getElementsByTagName("BiddingModel").getLength() > 0 &&
            element.getElementsByTagName("BiddingModel").item(0).getChildNodes().getLength() > 0) {
            this.setBiddingModel(BiddingModel.fromString(((Node) element.getElementsByTagName("BiddingModel").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("BroadMatchBid").getLength() > 0 ) {
            this.setBroadMatchBid(new GxBid((Element)element.getElementsByTagName("BroadMatchBid").item(0)));
        }
        if (element.getElementsByTagName("ContentMatchBid").getLength() > 0) {
            this.setContentMatchBid(new GxBid((Element)element.getElementsByTagName("ContentMatchBid").item(0)));
        }
        if (element.getElementsByTagName("EndDate").getLength() > 0) {
            this.setEndDate(new GxDate((Element)element.getElementsByTagName("EndDate").item(0)));
        }
        if (element.getElementsByTagName("ExactMatchBid").getLength() > 0) {
            this.setExactMatchBid(new GxBid((Element) element.getElementsByTagName("ExactMatchBid").item(0)));
        }
        if (element.getElementsByTagName("GxAdGroupId").getLength() > 0 &&
            element.getElementsByTagName("GxAdGroupId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxAdGroupId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxAdGroupId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external AdGroup id");
        }
        if (element.getElementsByTagName("BingAdGroupId").getLength() > 0 &&
            element.getElementsByTagName("BingAdGroupId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingAdGroupId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingAdGroupId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Language").getLength() > 0 &&
            element.getElementsByTagName("Language").item(0).getChildNodes().getLength() > 0) {
            this.setLanguage(((Node)element.getElementsByTagName("Language").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Name").getLength() > 0 &&
            element.getElementsByTagName("Name").item(0).getChildNodes().getLength() > 0) {
            this.setName(((Node)element.getElementsByTagName("Name").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Network").getLength() > 0 &&
            element.getElementsByTagName("Network").item(0).getChildNodes().getLength() > 0) {
            this.setNetwork(Network.fromString(((Node)element.getElementsByTagName("Network").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("PhraseMatchBid").getLength() > 0) {
            this.setPhraseMatchBid(new GxBid((Element)element.getElementsByTagName("PhraseMatchBid").item(0)));
        }
        if (element.getElementsByTagName("PricingModel").getLength() > 0 &&
            element.getElementsByTagName("PricingModel").item(0).getChildNodes().getLength() > 0) {
            this.setPricingModel(PricingModel.fromString(((Node) element.getElementsByTagName("PricingModel").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        /*NodeList publisherCountriesNode = element.getElementsByTagName("PublisherCountryCollection");
        if (publisherCountriesNode.getLength() > 0) {
            NodeList publisherCountryNode = ((Element)publisherCountriesNode.item(0)).getElementsByTagName("PublisherCountry");
            PublisherCountry[] publisherCountries = new PublisherCountry[publisherCountryNode.getLength()];
            for (int temp = 0; temp < publisherCountryNode.getLength(); temp++) {
                publisherCountries[temp] = new GxPublisherCountry((Element)publisherCountryNode.item(temp));
            }
            this.setPublisherCountries(publisherCountries);
        } */
        if (element.getElementsByTagName("StartDate").getLength() > 0) {
            this.setStartDate(new GxDate((Element)element.getElementsByTagName("StartDate").item(0)));
        }
        if (element.getElementsByTagName("Status").getLength() > 0 &&
            element.getElementsByTagName("Status").item(0).getChildNodes().getLength() > 0) {
            if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("E")) {
                this.setStatus(AdGroupStatus.Active);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("D")) {
                this.setStatus(AdGroupStatus.Deleted);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("DR")) {
                this.setStatus(AdGroupStatus.Draft);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("P")) {
                this.setStatus(AdGroupStatus.Paused);
            }
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
